package cn.macswelle.campusecurity.data_process.controller;

import cn.macswelle.campusecurity.common.dto.requestDto.FaceRegDto;
import cn.macswelle.campusecurity.common.dto.requestDto.RecordDto;
import cn.macswelle.campusecurity.common.dto.responseDto.LocationDto;
import cn.macswelle.campusecurity.data_process.receiver.AmqpReceiver;
import cn.macswelle.campusecurity.database.entities.Personnel;
import cn.macswelle.campusecurity.database.entities.Record;
import cn.macswelle.campusecurity.feignapi.deviceManager.RecordApi;
import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/data")
public class RecordController extends AmqpReceiver implements RecordApi {
  @Autowired
  private AipFace client;

  @RequestMapping(value = "/savePersonnel", method = RequestMethod.POST)
  @Override
  public String savePersonnel(int category, String name, String description) {
    Personnel personnel = new Personnel();
    personnel.setName(name);
    personnel.setDescription(description);
    personnel.setCategory(category);
    return personnelRepository.save(personnel).getId();
  }

  @RequestMapping(value = "/addRecord", method = RequestMethod.POST)
  @Override
  public String saveRecord(RecordDto dto) {
    Record record = new Record();
    record.setPersonnel(personnelRepository.findById(dto.getPersonnel()).orElse(null));
    record.setFile(dto.getFile());
    record.setLocation(locationRepository.findById(dto.getLocation()).orElse(null));
    record.setEvent(dto.getEvent());
    record.setCreateTime(dto.getCreateTime());
    return recordRepository.save(record).getId();
  }

  @RequestMapping(value = "/registerFace", method = RequestMethod.POST)
  @Override
  public String register(FaceRegDto faceRegDto) {
    Personnel personnel = new Personnel();
    personnel.setCategory(1);
    personnel.setDescription(faceRegDto.getUser_info());
    personnel.setName(faceRegDto.getName());
    String id = personnelRepository.save(personnel).getId();
    logger.info("register saved:" + id);
    HashMap<String, String> options = new HashMap<>();
    options.put("user_info", faceRegDto.getUser_info());
    options.put("quality_control", faceRegDto.getQuality_control());
    options.put("liveness_control", faceRegDto.getLiveness_control());
    options.put("action_type", faceRegDto.getAction_type());
    logger.info("registered:" + faceRegDto);
    JSONObject object = client.addUser(faceRegDto.getImage(), faceRegDto.getImage_type(),
      faceRegDto.getGroup_id(), id, options);
    logger.info("register:" + object.toString());
    if (!object.get("error_msg").equals("SUCCESS")) {
      personnelRepository.delete(personnel);
      logger.info("注册失败，已撤销之前操作");
      return null;
    }
    return id;
  }

  @RequestMapping(value = "/findPersonnel", method = RequestMethod.POST)
  @ResponseBody
  @Override
  public List<LocationDto> findPersonnel(String personnelId) {
    List<LocationDto> result = new ArrayList<>();
    for (Map<String, String> location : locationRepository.findByPersonnel(personnelId)) {
      LocationDto locationDto = new LocationDto();
      locationDto.setId(location.get("id"));
      locationDto.setName(location.get("name"));
      locationDto.setDescription(location.get("description"));
      locationDto.setTime(Long.parseLong(location.get("time")));
      locationDto.setFile(location.get("file"));
      result.add(locationDto);
    }
    return result;
  }


}
